export MY_FZF_DARK_COLOR='
  --color=hl:#72a4ff,hl+:#87ff00,pointer:#85ce32,prompt:#87ff00'

export MY_FZF_LIGHT_COLOR='
  --color=fg:#171421,bg:#ffffff,hl:#d7005f
  --color=fg+:#4d4d4c,bg+:#e8e8e8,hl+:#d7005f
  --color=info:#4271ae,prompt:#8959a8,pointer:#d7005f
  --color=marker:#4271ae,spinner:#4271ae,header:#4271ae'

function pick_term_program_color() {
    if [ "$1" ] && [ "$1" = "OSC11" ]; then
        _pick_term_program_color_OSC11
    else
        _pick_term_program_color_grep
    fi
}

function _set_program_color() {
    if [ "$1" ] && [ "$1" = "dark" ]; then
        # export BAT_THEME='TwoDark'
        export FZF_DEFAULT_OPTS=$MY_FZF_DEFAULT_OPTS$MY_FZF_DARK_COLOR
    else
        # export BAT_THEME='ansi'
        export FZF_DEFAULT_OPTS=$MY_FZF_DEFAULT_OPTS$MY_FZF_LIGHT_COLOR
    fi
}

function _pick_term_program_color_OSC11() {
    if [[ -z ${VIMRUNTIME} ]] && [[ -z ${INSIDE_EMACS} ]];then # no need set variable in vim build-in term or term opened in emacs
        if [ $(term-query-bg 2>/dev/null) = "dark" ]; then
            _set_program_color dark
        else
            _set_program_color
        fi
    fi
}

function _pick_term_program_color_grep() {
    if [[ -n ${ALACRITTY_WINDOW_ID} ]]; then
        grep  'tango_theme' ~/.config/alacritty/alacritty.toml &> /dev/null
        if [ $? -eq 0 ]; then
            _set_program_color dark
        else
            _set_program_color
        fi
    fi
    if [[ -n ${GNOME_TERMINAL_SCREEN} ]]; then
        _set_program_color
    fi
}

pick_term_program_color
